﻿using Business;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookManageSys.User
{
    public partial class UserCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            try
            {
                CartsEntity ce = new CartsEntity();
                ce.Username = Session["UserName"].ToString();

                CartsBusiness cb = new CartsBusiness();
                DataTable dt = cb.GetAllData(ce);

                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                CartsEntity ce = new CartsEntity();
                ce.Username = Session["UserName"].ToString();
                ce.Productname = TxtSearch.Text;

                CartsBusiness cb = new CartsBusiness();
                DataTable dt = cb.SearchAllData(ce);

                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }

        protected void BtnBuy_Click(object sender, EventArgs e)
        {
            decimal Amount = 0;

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkSelect = (CheckBox)row.FindControl("ChkSelect");

                if (chkSelect.Checked)
                {
                    int quantity = Convert.ToInt32(row.Cells[4].Text);
                    decimal price = Convert.ToDecimal(row.Cells[5].Text);

                    Amount += quantity * price;
                }
            }

            if (Amount > 0)
            {
                Panel1.Visible = true;
                LblAmount.Text = Amount.ToString(); ;
                BindUserInfo();
            }
            else
            {
                Response.Write("<script>alert('请选择购买的商品！')</script>");
            }
        }

        private void BindUserInfo()
        {
            try
            {
                UsersEntity ue = new UsersEntity();
                ue.Username = Session["UserName"].ToString();

                UsersBusiness ub = new UsersBusiness();
                DataTable dt = ub.GetUserInfo(ue);

                TxtUserName.Text = dt.Rows[0]["username"].ToString();
                TxtAddress.Text = dt.Rows[0]["address"].ToString();
                TxtTelephone.Text = dt.Rows[0]["telephone"].ToString();
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }

        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            string sql = "";
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkSelect = (CheckBox)row.FindControl("ChkSelect");

                if (chkSelect.Checked)
                {
                    string data = row.Cells[1].Text;
                    sql += " or cartid = " + data;
                }
            }
            CartsBusiness cb = new CartsBusiness();
            int res = cb.DeleteCart(sql);

            if (res > 0)
            {
                Response.Write("<script>alert('删除成功！')</script>");
                BindData();
            }
            else
            {
                Response.Write("<script>alert('删除失败！')</script>");
            }
        }

        protected void ChkAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkSelect = (CheckBox)row.FindControl("ChkSelect");
                if (ChkAll.Checked)
                {
                    chkSelect.Checked = true;
                }
                else
                {
                    chkSelect.Checked = false;
                }
            }
        }

        protected void BtnConfirmBuy_Click(object sender, EventArgs e)
        {
            try
            {
                OrdersEntity oe = new OrdersEntity();
                oe.Username = Session["UserName"].ToString();
                oe.Amount = Convert.ToDecimal(LblAmount.Text);
                oe.Orderdate = DateTime.Now;

                OrdersBusiness ob = new OrdersBusiness();
                int res1 = ob.AddOrder(oe);

                if (res1 > 0)
                {
                    object orderID = ob.GetMaxOrderID();

                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        CheckBox chkSelect = (CheckBox)row.FindControl("ChkSelect");

                        if (chkSelect.Checked)
                        {
                            OrderdetailsEntity ode = new OrderdetailsEntity();
                            ode.Orderid = Convert.ToInt32(orderID);
                            ode.Username = Session["UserName"].ToString();
                            ode.Productname = row.Cells[3].Text;
                            ode.Quantity = Convert.ToInt32(row.Cells[4].Text);
                            ode.Amount = Convert.ToInt32(row.Cells[4].Text) * Convert.ToDecimal(row.Cells[5].Text);

                            OrderdetailsBusiness odb = new OrderdetailsBusiness();
                            int res2 = odb.AddOrderdetail(ode);

                            if (res2 > 0)
                            {
                                ProductsEntity pe = new ProductsEntity();
                                pe.Productname = row.Cells[3].Text;
                                pe.Salesquantity = Convert.ToInt32(row.Cells[4].Text);

                                ProductsBusiness pb = new ProductsBusiness();
                                int res3 = pb.ResetProductByProductName(pe);
                                BindData();
                                Panel1.Visible = false;
                            }
                            else
                            {
                                Response.Write("<script>alert('购买失败！')</script>");
                            }
                        }
                    }
                    
                }
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }

        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}